package com.eascs.spider.hr.config;

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.eascs.ScanIndex;
import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.ConnectionPoolDataSource;
import com.eascs.app.dao.DataSourceSetting;
import com.eascs.app.dao.JdbcSession;
import com.eascs.common.dfs.fastdfs.FastDfsClient;
import com.eascs.common.ftp.FtpConfig;
import com.eascs.web.DefaultAppConfig;

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ComponentScan(basePackageClasses = { ScanIndex.class })
public class AppConfig extends DefaultAppConfig {
	@Bean
	public DataSourceSetting ticketDataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db1.");
		return dataSourceSetting;
	}

	@Bean
	public FastDfsClient fastDfsClient() {
		return new FastDfsClient();
	}

	@Bean
	public DataSource workflowDataSource() {
		DataSourceSetting dataSourceSetting = ticketDataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean(name = "ticketTransactionManager")
	public PlatformTransactionManager transactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(workflowDataSource());
		return transactionManager;
	}

	@Bean(name = "ticketJdbcSession")
	public JdbcSession jdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.workflowDataSource());
		return jdbcSession;
	}

	/*
	 * @Bean public DubboConsumerInterceptor dubboConsumerInterceptor() { return
	 * new DubboConsumerInterceptor(); }
	 */
	@Bean
	public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Bean
	public FtpConfig ftpConfig() {
		FtpConfig ftpConfig = new FtpConfig();
		ConfigContext.bindConfigObject(ftpConfig);
		return ftpConfig;

	}
}
